package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryBizSceneReq;
import com.sg.dto.base.framework.req.QueryBizSceneDetailReq;
import com.sg.dto.base.framework.req.QueryBizSceneListReq;
import com.sg.entity.BizScene;
import com.sg.service.base.framework.MBizSceneService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.BizSceneMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MBizSceneServiceImpl
        extends ServiceImpl<BizSceneMapper, BizScene>
        implements MBizSceneService {

    @Autowired
    BizSceneMapper bizSceneMapper;

    /**
     * 1-1-09查询业务场景列表[1416]
     * gen by moon at 9/13/2022, 3:32:29 AM
     */
    @Trace(operationName = "1-1-09查询业务场景列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BizScene> queryBizSceneList(QueryBizSceneListReq reqDto) {

        try {
            QueryWrapper<BizScene> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(BizScene.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(BizScene.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(BizScene.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<BizScene>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-09批量查询业务场景列表[225]
     * gen by moon at 9/13/2022, 3:32:28 AM
     */
    @Trace(operationName = "1-1-09批量查询业务场景列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BizScene> batchQueryBizScene(BatchQueryBizSceneReq reqDto) {

        try {
            QueryWrapper<BizScene> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getBizSceneList() == null || reqDto.getBizSceneList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("biz_scene_id", reqDto.getBizSceneList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(BizScene.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(BizScene.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(BizScene.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<BizScene>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-09查询业务场景详情[40]
     * gen by moon at 9/13/2022, 3:32:28 AM
     */
    @Trace(operationName = "1-1-09查询业务场景详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BizScene queryBizSceneDetail(QueryBizSceneDetailReq reqDto) {

        try {

            QueryWrapper<BizScene> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<BizScene> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                BizScene retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-09删除业务场景	[39]
     * gen by moon at 9/13/2022, 3:32:27 AM
     */
    @Trace(operationName = "1-1-09删除业务场景	")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBizScene(String bizSceneId) {

        return super.removeById(bizSceneId);
    }

    /**
     * 1-1-09修改业务场景	[38]
     * gen by moon at 9/13/2022, 3:32:27 AM
     */
    @Trace(operationName = "1-1-09修改业务场景	")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBizScene(BizScene bizScene) {
        bizScene.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (bizScene != null && bizScene.getSubjectLifeCycle() != null && bizScene.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            bizScene.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (bizScene != null && bizScene.getSubjectLifeCycle() != null && bizScene.getSubjectLifeCycle().equals("ARCHIVING")) {
            bizScene.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        bizScene.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (bizScene != null && bizScene.getSubjectLifeCycle() != null && bizScene.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            bizScene.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (bizScene != null && bizScene.getSubjectLifeCycle() != null && bizScene.getSubjectLifeCycle().equals("ARCHIVING")) {
            bizScene.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(bizScene);
    }

    /**
     * 1-1-09新增业务场景[37]
     * gen by moon at 9/13/2022, 3:32:26 AM
     */
    @Trace(operationName = "1-1-09新增业务场景")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addBizScene(BizScene bizScene) {
        bizScene.setBizSceneId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        bizScene.setIsArchive("FALSE");
        bizScene.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        bizScene.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        bizScene.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (bizScene != null && bizScene.getSubjectLifeCycle() != null && bizScene.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            bizScene.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        bizScene.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (bizScene != null && bizScene.getSubjectLifeCycle() != null && bizScene.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            bizScene.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        bizScene.setIsValid("FALSE");
        bizScene.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        bizScene.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(bizScene)) {
            return bizScene.getBizSceneId();
        } else {
            return "-1";
        }

    }
}
